﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Shared;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ElectronicShoppingMall.Service.Services.Client
{
    public class GoodsService:IDisposable
    {
        private ElectronicShoppingMallContext data;
        public GoodsService()
        {
            data = new ElectronicShoppingMallContext();
        }
        /// <summary>
        /// 根据商品分类获取上架的商品信息
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public List<GoodsInfo> GetAllGoodsByCategory(String category)
        {
            return data.GoodsInfoes.Where(g => g.Category == category&& g.State == 0).ToList();
        }
        /// <summary>
        /// 获取商品Id获取商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public GoodsInfo GetGoodsInfoByGoodsId(int id)
        {
            return data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == id);
        }
        /// <summary>
        /// 获取购物车商品信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Dictionary<GoodsInfo, int> GetCartGoodsInfoByUserId(int userId)
        {
            Dictionary<GoodsInfo, int> result = new Dictionary<GoodsInfo, int>();
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var carts = client.ShoppingCarts.ToList();
            foreach (var cart in carts)
            {
                result.Add(cart.GoodsInfoes.First(), cart.Number);
            }
            return result;
        }
        /// <summary>
        /// 获取用户默认快递信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DeliveryInfo GetClientDeliveryInfoByUserId(int userId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            return client.DeliveryInfoes.SingleOrDefault(d => d.IsDefault == true);
        }
       /// <summary>
       /// 添加商品到购物车
       /// </summary>
       /// <param name="userId"></param>
       /// <param name="goodsId"></param>
       /// <param name="number"></param>
       /// <returns></returns>
        public string AddGoodsToCart(int userId, int goodsId, int number)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var goodsInfo = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            var goods = client.ShoppingCarts.SingleOrDefault(c => c.GoodsInfoes.First() == goodsInfo);
            if (goods != null && number < goodsInfo.Stock)
            {
                goods.Number += number;
                data.SaveChanges();
                return "添加成功";
            }
            else if(number < goodsInfo.Stock)
            {
                ShoppingCart cart = new ShoppingCart
                {
                    ClientId = client.ClientId,
                    CreateTime = DateTime.Now,
                    Number = number
                };
                cart.GoodsInfoes.Add(goodsInfo);
                data.ShoppingCarts.Add(cart);
                data.SaveChanges();
                return "添加成功";
            }
            return "添加失败";
        }
        /// <summary>
        /// 删除商品从购物车
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="goodsId"></param>
        public void DeleteGoodsFromCart(int userId, int goodsId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsId);
            var cart = client.ShoppingCarts.SingleOrDefault(c => c.GoodsInfoes.First() == goods);
            cart.GoodsInfoes.Clear();
            data.ShoppingCarts.Remove(cart);
            data.SaveChanges();
        }
        /// <summary>
        /// 获取新品推荐
        /// </summary>
        /// <returns></returns>
        public List<NewProductModel> GetNewProduct()
        {
            List<NewProductModel> result = new List<NewProductModel>();
           var products =  data.NewProducts.ToList();
            foreach(var product in products)
            {
                var image = data.Images.SingleOrDefault(i => i.ImageId == product.ImageId);
                NewProductModel temp = new NewProductModel
                {
                    Id = product.Id,
                    Image = image.ImagePath,
                    Url = product.Url
                };
                result.Add(temp);
            }
            return result;
        }

        public void Dispose()
        {
            data.Dispose();
        }
    }
}
